const notifyToast = Swal.mixin({
    toast: true,
    position: 'center',
    background: '#ffffe1',
    showConfirmButton: false,
    backdrop: false,
    timer: 3000
});

function notifyOnSendClick() {
    var textContent = $('#text-area-notify').val();

    if (textContent.trim() == '') {
        notifyToast.fire({
            type: 'error',
            title: '請輸入消息內容'
        });
        return;
    }

    $.ajax(
        {
            type: 'post',
            async: true,
            data: {'message': textContent},
            url: document.location.origin + '/api/sendnotify',
            dataType: 'text',
            error: function () {
                notifyToast.fire({
                    type: 'error',
                    title: "Ajax 发生错误！"
                });
            },
            success: function (data) {
                var json = JSON.parse(data);

                if (json.result == 200) {
                    notifyToast.fire({
                        type: 'success',
                        title: '消息發送成功'
                    });
                } else {
                    notifyToast.fire({
                        type: 'error',
                        title: json.description
                    });
                }
            }
        }
    );
}